package com.intelligent.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.pojo.Equipment;
import com.intelligent.service.EquipmentService;
import com.intelligent.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Resource
    private EquipmentService equipmentService;

    /**
     * 分页查询设备（关联用户表）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @param equipmentCode 设备编号（模糊匹配，可选）
     * @param equipmentName 设备名称（模糊匹配，可选）
     * @param username 设备负责人用户名（模糊匹配，可选）
     * @param status 状态 0启用 1禁用（可选）
     * @return 分页数据，包含设备负责人用户名
     */
    @GetMapping("/page")
    public Result<IPage<Equipment>> page(@RequestParam(defaultValue = "1") long pageNo,
                                         @RequestParam(defaultValue = "10") long pageSize,
                                         @RequestParam(required = false) String equipmentCode,
                                         @RequestParam(required = false) String equipmentName,
                                         @RequestParam(required = false) String username,
                                         @RequestParam(required = false) Integer status) {
        Page<Equipment> page = new Page<>(pageNo, pageSize);
        IPage<Equipment> result = equipmentService.pageEquipment(page, equipmentCode, equipmentName, username, status);
        return Result.success(result);
    }

    /**
     * 根据ID查询设备（关联用户表）
     * @param equipmentId 设备ID
     * @return 设备信息，包含设备负责人用户名
     */
    @GetMapping("/{equipmentId}")
    public Result<Equipment> getById(@PathVariable Long equipmentId) {
        Equipment equipment = equipmentService.getEquipmentById(equipmentId);
        if (equipment == null) {
            return Result.error("设备不存在");
        }
        return Result.success(equipment);
    }

    /**
     * 添加设备（设备编号自动生成）
     * @param equipment 设备信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody Equipment equipment) {
        boolean success = equipmentService.addEquipment(equipment);
        return success ? Result.success("设备添加成功") : Result.error("设备添加失败");
    }

    /**
     * 修改设备
     * @param equipment 设备信息
     * @return 修改结果
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody Equipment equipment) {
        boolean success = equipmentService.updateEquipment(equipment);
        return success ? Result.success("设备修改成功") : Result.error("设备修改失败");
    }

    /**
     * 删除设备
     * @param equipmentId 设备ID
     * @return 删除结果
     */
    @DeleteMapping("/{equipmentId}")
    public Result<String> delete(@PathVariable Long equipmentId) {
        // 参数校验
        if (equipmentId == null) {
            return Result.error("设备ID不能为空");
        }

        boolean success = equipmentService.deleteEquipment(equipmentId);
        if (success) {
            return Result.success("设备删除成功");
        } else {
            return Result.error("设备删除失败");
        }
    }
}
